package cn.yl.fxboot.application.controller;

import java.net.URL;
import java.util.ResourceBundle;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.yl.fxboot.FxApplication;
import cn.yl.fxboot.application.common.utils.EncryptUtil;
import cn.yl.fxboot.application.view.MainStageView;
import de.felixroske.jfxsupport.FXMLController;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
@FXMLController
public class LoginStageController implements Initializable{
	@FXML
	private TextField pwd;
	@FXML
	private ImageView qrcode;
	
	@FXML
	private Label msgLabel;
	
	private static final String key = "yyqyylyytylywtal";
	
	private EncryptUtil util = EncryptUtil.getInstance();

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		Image image = new Image("image/qrcode.png");
		qrcode.setImage(image);
	}
	/***
	 * 
	 * 
	 * @param e
	 */
	public void login(Event e) {
		Task<Void> task = new Task<Void>() {
			
			@Override
			protected void running() {
				updateMessage("正在登录...");
			}

			@Override
			protected Void call() throws Exception {
				String token = pwd.getText();
				String url = "http://www.uzivn.cn:8081/token/"+token;
				String res = HttpRequest.get(url).execute().body();
				
				JSONObject json = (JSONObject) JSONUtil.parse(res);
				if(json.getInt("code") == 401) {
					updateMessage("密钥已过期");
					Platform.runLater(() ->{
					Alert alert = new Alert(AlertType.ERROR);
					alert.setContentText("密钥已过期");
					alert.setHeaderText("");
					alert.show();
				});
				}
//				/json.getInt("code") == 200
				if(json.getInt("code") == 200) {
					String data = "1";
					String d = json.getStr("data");
					String sourceData = util.XORdecode(d, key);
					String[] strs = sourceData.split(";");
					if(strs.length>0 && data.equals(strs[0])) {//strs.length>0 && data.equals(strs[0])
						Platform.runLater(() ->{
							Stage s = (Stage) pwd.getScene().getWindow();
							s.close();
							FxApplication.showView(MainStageView.class);
						});
					}else {
						updateMessage("密钥不正确或已过期");
						Platform.runLater(() ->{
							Alert alert = new Alert(AlertType.ERROR);
							alert.setContentText("密钥不正确");
							alert.setHeaderText("");
							alert.show();
						});
					}
				}
				return null;
			}
			
		};
		task.messageProperty().addListener(new ChangeListener<String>() {
			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				msgLabel.setText(newValue);
			}
		});
		new Thread(task).start();
	}
	
	
}
